home *** CD-ROM | disk | FTP | other *** search
- /*
- File: CAS_Doc.c
-
- Contains: Document window and file handling routines.
-
- Written by: David H Nelson
-
- Copyright © 1993-1995 ComponentWorks, All rights reserved.
-
- Change History (most recent first):
-
- 05/15/95 DAS moved common drag handler and CALib callback
- installation code into individual routines
- --------------------------------------
- 05/15/95 RB Adding CALib Install calls for "Container" handlers
- --------------------------------------
- 05/09/95 SJF Tweaks for WWDC Demo
- --------------------------------------
-
- 05/08/95 SJF Reenable Drag handler install section, release
- handlers BEFORE closing doc...
- --------------------------------------
-
- 04/23/95 RB Modified Doc_New & Doc_Open to register register
- focus handlers with CALib.
- --------------------------------------
-
- 04/22/95 DAS cleaned up and added error checking.
- --------------------------------------
- 03/29/95 RB Changed DocPtr->fRefNum initial value from -1 back
- to 0. There's code which depends on 0 as the initial
- value.
-
- Fixed Doc_New & Doc_Open to make sure the root frame
- of a window is clipped to the cont area - scrollbars.
- --------------------------------------
- 03/27/95 DAS improved document clean/dirty handling,
- as per Rick/Steve requests.
- --------------------------------------
- 03/17/95 RB Added support for Bento storage model
- --------------------------------------
- 01/25/95 RB Added support for internal scrap format
- --------------------------------------
- 01/20/95 RB Added docMouseInItem()
- --------------------------------------
-
- 01/18/95 DHN Global function and variable name changes to suggest
- OOP design. Split app.c file into app.c, win.c, doc.c,
- item.c, and util.c.
- --------------------------------------
- 01/17/95 DAS changed all FrontWindow() calls to
- App_GetFrontDocWindow() to account for
- floating windows. Added Win_IsToolWindow().
- 1/16/95 DHN Massive improvements including, multiple window
- support, new window offsetting and naming, pref
- file support, file saving/reading, cursor tracking,
- PowerPC support, window update/activate in dialog
- filter, item drawing/selection/cut/copy (not paste),
- drag & drop (no drop yet), window scrolling, get
- info dialog, Select all menu item, Tools menu.
- through
- 12/23/94 DHN Created CASample from existing Light Software
- application framework.
- --------------------------------------
- 1/24/93 DHN Added code to ignore the enter key. Fixed page
- flipping animation by using current GrafPort in
- local coords rather than new GrafPort with global
- coords. Fixed findAgain to actually do something.
- 11/20/93 DHN Created.
- */
-
- #ifdef USE_CALIB
- #include "CALib.h"
- #include "CAS_CAUtil.h"
- #endif
-
- #include "CAS_Globals.h"
- #include "CAS_Error.h"
- #include "CAS_Doc.h"
- #include "CAS_Win.h"
- #include "CAS_App.h"
- #include "CAS_StringTools.h"
- #include "CAS_Content.h"
-
- #include <string.h>
-
- void GetPlatformScrapTypeList (ResType typeArray[]);
-
- //----------------------------------------------------------------------
- // local prototypes
-
- #if defined(__cplusplus)
- extern "C"
- {
- #endif
-
- static OSErr Doc_InstallDragHandlers(
- DocPtr theDoc,
- WindowPtr theWindow );
- static OSErr Doc_InstallCALibHandlers(
- DocPtr theDoc,
- WindowPtr theWindow,
- Boolean isNewDoc );
-
- #if defined(__cplusplus)
- }
- #endif
-
-
- //----------------------------------------------------------------------
- // Doc_InitData - initialize DocData instance.
-
- void Doc_InitData(
- DocPtr theDoc )
- {
- Rect emptyR;
-
- if (theDoc != nil)
- {
- SetRect( &emptyR, 0, 0, 0, 0 );
-
- theDoc->fileRefNum = (0);
- theDoc->fileLocked = false;
- theDoc->filler1 = false;
-
- theDoc->changed = false;
-
- theDoc->hPrint = nil;
-
- theDoc->hScrollBar = nil;
- theDoc->vScrollBar = nil;
-
- theDoc->hScrollBarRect = emptyR;
- theDoc->vScrollBarRect = emptyR;
-
- theDoc->contentRect = emptyR;
- theDoc->documentRect = emptyR;
-
- theDoc->undoData = nil;
-
- theDoc->contentColl = (ElemCollPtr) NewPtr (sizeof (ElemColl));
- ElemColl_Init(theDoc->contentColl, theDoc);
-
- theDoc->insertionPoint.v = 20;
- theDoc->insertionPoint.h = 20;
-
- }
- }
-
-
- //----------------------------------------------------------------------
- // Doc_GetDirty - called to find out if the doc has been modified.
-
- Boolean Doc_GetDirty(
- DocPtr theDoc )
- {
- Boolean retVal = false;
-
- if (theDoc == nil)
- return false;
-
- #ifdef USE_CALIB
-
- retVal = CAHasDocumentChanged (theDoc->partDocRef);
-
- #endif
-
- if (theDoc->changed) retVal = true;
-
- return (retVal);
-
- }
-
-
- //----------------------------------------------------------------------
- // Doc_SetDirty - called whenever the document changes. It sets the
- // doc's changed field as specified and does anything else the app needs.
-
- void Doc_SetDirty(
- DocPtr theDoc,
- Boolean isDirty )
- {
-
- if (theDoc != nil)
- {
- if (isDirty != Doc_GetDirty (theDoc))
- {
- theDoc->changed = isDirty;
- App_AdjustMenus (Doc_GetWindow (theDoc));
- }
-
- }
- }
-
-
- //---------------------------------------------------------------------------
- // Set the coordinate system to that of our document’s, based on the scroll bars.
-
- void Doc_SetGrafOrigin(
- DocPtr theDoc )
- {
- Point docOrigin;
-
- docOrigin.h = 0;
- docOrigin.v = 0;
-
- if (theDoc != nil)
- {
- if (theDoc->hScrollBar != nil)
- docOrigin.h = GetControlValue( theDoc->hScrollBar );
- if (theDoc->vScrollBar != nil)
- docOrigin.v = GetControlValue( theDoc->vScrollBar );
- }
-
- SetOrigin( docOrigin.h, docOrigin.v );
- }
-
- //---------------------------------------------------------------------------
- // Doc_AddItemPict - Add an item to our document at thePt (doc coords)
- // using the picture thePict.
-
- ElemPtr Doc_AddItemPict(
- DocPtr theDoc,
- PicHandle thePict,
- Point thePt )
- {
- Rect theRect;
- GrafPtr savePort;
- OSErr theErr;
- ElemPtr theElem;
-
- GetPort( &savePort );
- SetPort( Doc_GetWindow( theDoc ) );
-
- HLock ((Handle) thePict);
- theRect = (**(thePict)).picFrame;
-
- thePt.h += theRect.left - (theRect.right - theRect.left)/2;
- thePt.v += theRect.top - (theRect.bottom - theRect.top)/2;
-
- theElem = Elem_New ((Handle) thePict, kPICTType, theRect, thePt);
- ElemColl_AddElem (theDoc->contentColl, theElem);
-
- HUnlock ((Handle) thePict);
-
- Elem_SetVisible (theElem, true);
-
- // draw the item, unselected.
-
- Doc_SetDirty( theDoc, true );
- SetPort( savePort );
-
- return (theElem);
- }
-
- //---------------------------------------------------------------------------
- // Doc_AddItemsFromScrap -
-
- void Doc_AddItemsFromScrap(
- DocPtr theDoc,
- Handle theScrap,
- Point origin )
- {
- PicHandle aPicture;
- Ptr offsetPtr;
- Point location, sourceOrigin;
- OSErr theErr;
- long pictSize, pictCount, i;
- ElemPtr elem;
-
- offsetPtr = *theScrap;
- BlockMoveData( offsetPtr, (Ptr)&sourceOrigin, (long)sizeof(Point) );
- offsetPtr += sizeof(Point);
-
- BlockMoveData( offsetPtr, (Ptr)&pictCount, (long)sizeof(long) );
- offsetPtr += sizeof(long);
-
- Doc_SelectAllElements( theDoc, false );
-
- for (i=0; i<pictCount; ++i)
- {
- BlockMoveData( (Ptr)offsetPtr, (Ptr)&location, (long)sizeof(Point) );
- offsetPtr += sizeof(Point);
-
- BlockMoveData( (Ptr)offsetPtr, (Ptr)&pictSize, (long)sizeof(long) );
- offsetPtr += sizeof(long);
-
- // New location is the new origin + the relative offset
- // of the item
- location.h = origin.h + (location.h - sourceOrigin.h);
- location.v = origin.v + (location.v - sourceOrigin.v);
-
- aPicture = (PicHandle)NewHandle( pictSize );
- theErr = MemError();
- if (theErr == noErr)
- {
- BlockMoveData( (Ptr)offsetPtr, *aPicture, pictSize );
- offsetPtr += pictSize;
-
- // Adjust the location
- elem = Doc_AddItemPict( theDoc, aPicture, location );
- Elem_SetSelected (elem, true);
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Doc_SelectionExists - if any item is selected, return true.
-
- Boolean Doc_SelectionExists(
- DocPtr theDoc )
- {
- short count, i;
- ElemPtr elem;
- Boolean retVal = false;
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- if (Elem_GetSelected (elem))
- {
- retVal = true;
- break;
- }
- }
-
- return retVal;
- }
-
-
- //---------------------------------------------------------------------------
- // Doc_GetSelectedFrameCount
-
- short Doc_GetSelectedFrameCount(
- DocPtr theDoc )
- {
- short count, i;
- short selectedCount = 0;
- ElemPtr elem;
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- if (Elem_GetSelected (elem) && (Elem_GetType (elem) == kFrameElemType))
- {
- ++selectedCount;
- }
- }
-
- return selectedCount;
- }
-
-
- //---------------------------------------------------------------------------
- // Set the select flag on all the items in the window's document to bSelect.
- // Also inval the rects of the items so they are redrawn.
- // Origin is not important and may be set to 0, 0 upon exit.
-
- void Doc_SelectAllElements(
- DocPtr theDoc,
- Boolean bSelect )
- {
- short count, i;
- ElemPtr elem;
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- if (Elem_GetSelected (elem) != bSelect)
- {
- Elem_SetSelected (elem, bSelect);
-
- }
- }
-
- ElemColl_ClipElements(theDoc->contentColl, NULL);
-
- }
-
-
- //---------------------------------------------------------------------------
- // deletes all items that are selected from the window's document.
- // Also inval the rects of the items so they are redrawn.
- // Origin is not important and will be set to 0, 0 upon exit.
-
- void Doc_DeleteElements(
- DocPtr theDoc, ElemListPtr selectionList )
- {
- Point saveOrigin;
- long i;
- short count;
- ElemPtr elem;
- GrafPtr savePort;
-
- GetPort (&savePort);
- SetPort (Doc_GetWindow(theDoc));
-
- // move the coordinate system according to the scroll bars.
- GetOrigin( &saveOrigin );
- Doc_SetGrafOrigin( theDoc );
-
- count = ElemList_GetCount (selectionList);
-
- for (i=count; i >= 1; i--)
- {
- elem = ElemList_GetNthElem (selectionList, i);
- ElemColl_RemoveElem (theDoc->contentColl, elem);
- }
-
- // set the coordinate system back.
- SetOrigin( saveOrigin.h, saveOrigin.v );
- SetPort (savePort);
-
- }
-
-
- //---------------------------------------------------------------------------
- // deletes all items that are selected from the window's document.
- // Also inval the rects of the items so they are redrawn.
- // Origin is not important and will be set to 0, 0 upon exit.
-
- void Doc_DeleteCurrentSelection(
- DocPtr theDoc )
- {
- Point saveOrigin;
- long i;
- short count;
- ElemPtr elem;
- GrafPtr savePort;
-
- GetPort (&savePort);
- SetPort (Doc_GetWindow(theDoc));
-
- // move the coordinate system according to the scroll bars.
- GetOrigin( &saveOrigin );
- Doc_SetGrafOrigin( theDoc );
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=count; i >= 1; i--)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem))
- {
- ElemColl_RemoveElem (theDoc->contentColl, elem);
- }
- }
-
- // set the coordinate system back.
- SetOrigin( saveOrigin.h, saveOrigin.v );
- SetPort (savePort);
-
- }
-
- //---------------------------------------------------------------------------
- // Move all the selected items by offsetH pixels to the right and offsetV pixels down.
- // Also inval the rects of the items original and final location so they are redrawn.
- // Origin is not important and will be set to 0, 0 upon exit.
-
- void Doc_MoveSelection(
- DocPtr theDoc,
- short offsetH,
- short offsetV )
- {
- Point location;
- Point saveOrigin;
- long i;
- short count;
- ElemPtr elem;
-
- // move the coordinate system according to the scroll bars.
- GetOrigin( &saveOrigin );
- Doc_SetGrafOrigin( theDoc );
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=count; i >= 1; i--)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem))
- {
- Elem_GetLocation (elem, &location);
- location.v += offsetV;
- location.h += offsetH;
- Elem_SetLocation (elem, location);
- Elem_UpdateCAVisFrame(elem);
-
- }
-
- }
- // set the coordinate system back.
- SetOrigin( saveOrigin.h, saveOrigin.v );
- }
-
- //---------------------------------------------------------------------------
- // return the bounding box of all the selected items in document coords.
- // This routine should not be called unless there is at least one item selected.
- // It will return an empty rect in this case.
-
- void Doc_CalcSelectionBounds(
- DocPtr theDoc,
- Rect *boundsRect )
- {
- Rect theRect;
- long i;
- short count;
- ElemPtr elem;
-
- // loop through all the items
- // if the item is selected, add it to the boundsRect.
- SetRect( boundsRect, 0, 0, 0, 0 );
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem))
- {
- Elem_GetLocatedRect (elem, &theRect);
-
- if (EmptyRect (boundsRect))
- *boundsRect = theRect;
- else
- UnionRect( boundsRect, &theRect, boundsRect );
- }
-
- }
-
- }
-
- //---------------------------------------------------------------------------
- // return the bounding box of all items, in document coords.
-
- void Doc_CalcContentBounds(
- DocPtr theDoc,
- Rect *boundsRect )
- {
- Rect theRect;
- long i;
- short count;
- ElemPtr elem;
-
- // loop through all the items
- // if the item is selected, add it to the boundsRect.
- SetRect( boundsRect, 0, 0, 0, 0 );
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- Elem_GetLocatedRect (elem, &theRect);
-
- if (EmptyRect (boundsRect))
- *boundsRect = theRect;
- else
- UnionRect( boundsRect, &theRect, boundsRect );
-
- }
-
- }
-
-
-
- //---------------------------------------------------------------------------
- // Return the region of all the selected items in document coords.
- // This routine should not be called unless there is at least one item selected.
- // It will return an empty rect in this case.
-
- void Doc_CalcSelectionRgn(
- DocPtr theDoc,
- RgnHandle theRgn )
- {
- RgnHandle itemRgn;
- Rect itemRect;
- long i;
- long count;
- ElemPtr elem;
-
- SetRect( &itemRect, 0, 0, 0, 0 );
- RectRgn( theRgn, &itemRect );
-
- itemRgn = NewRgn();
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem))
- {
- Elem_GetLocatedRect (elem, &itemRect);
- RectRgn( itemRgn, &itemRect );
- UnionRgn( theRgn, itemRgn, theRgn );
- }
-
- }
-
- DisposeRgn (itemRgn);
-
- }
-
-
- //---------------------------------------------------------------------------
- // Return the region of all the selected items in document coords.
- // This routine should not be called unless there is at least one item selected.
- // It will return an empty rect in this case.
-
- void Doc_CalcContentElemRgn(
- DocPtr theDoc,
- RgnHandle theRgn )
- {
- RgnHandle itemRgn;
- Rect itemRect;
- short i, count;
- ElemPtr elem;
-
- SetRect( &itemRect, 0, 0, 0, 0 );
- RectRgn( theRgn, &itemRect );
-
- itemRgn = NewRgn();
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- Elem_GetLocatedRect (elem, &itemRect);
- RectRgn( itemRgn, &itemRect );
- UnionRgn( theRgn, itemRgn, theRgn );
-
- }
-
- DisposeRgn( itemRgn );
-
- }
-
-
- //---------------------------------------------------------------------------
-
- Handle Doc_CreateScrapFromSelection(
- DocPtr theDoc )
- {
- Handle scrapData;
- Ptr scrapOffset;
- Rect boundsRect;
- Point origin;
- Point location;
- OSErr theErr;
- long itemCount, scrapSize, pictSize, i;
- ElemPtr elem;
- short pictElemCount;
-
-
- itemCount = 0L;
- scrapSize = 0L;
-
- Doc_CalcSelectionBounds( theDoc, &boundsRect );
- origin.h = boundsRect.left;
- origin.v = boundsRect.top;
-
- pictElemCount = ElemColl_GetCountOfType (theDoc->contentColl, kPICTType);
-
- for (i=1; i<=pictElemCount; i++)
- {
-
- elem = ElemColl_GetNthElemOfType (theDoc->contentColl, i, kPICTType);
-
- if (Elem_GetSelected (elem))
- {
- ++itemCount;
- scrapSize += GetHandleSize( (Handle) elem->elemData );
-
- }
- }
-
- scrapSize +=
- sizeof(Point) + sizeof(long)
- + (itemCount * (sizeof(Point) + sizeof(long)));
- scrapData = NewHandle( scrapSize );
- theErr = MemError();
- if (theErr != noErr)
- return nil;
-
- scrapOffset = *scrapData;
-
- BlockMoveData( &origin, scrapOffset, (long)sizeof(Point) );
- scrapOffset += sizeof(Point);
-
- BlockMoveData( &itemCount, scrapOffset, (long)sizeof(long) );
- scrapOffset += sizeof(long);
-
- pictElemCount = ElemColl_GetCountOfType (theDoc->contentColl, kPICTType);
-
- for (i=1; i<=pictElemCount; i++)
- {
-
- elem = ElemColl_GetNthElemOfType (theDoc->contentColl, i, kPICTType);
-
- if (Elem_GetSelected (elem))
- {
- pictSize = GetHandleSize( (Handle)elem->elemData );
- Elem_GetLocation (elem, &location);
- BlockMoveData(
- (Ptr)&location,
- (Ptr)scrapOffset, (long)sizeof(Point) );
- scrapOffset += sizeof(Point);
-
- BlockMoveData(
- (Ptr)&pictSize, (Ptr) scrapOffset,
- (long)sizeof(long) );
- scrapOffset += sizeof(long);
-
-
- }
- }
-
- // loop through all the items, drawing the selected ones.
- // if the item is selected, draw it.
-
- return scrapData;
- }
-
-
- //---------------------------------------------------------------------------
- // return a picture containing all the selected items.
- // This routine should not be called unless there is at least one item selected.
- // It will return nil in this case.
-
- PicHandle Doc_CreateSelectionPicture(
- DocPtr theDoc )
- {
- Rect boundsRect;
- Point saveOrigin;
- PicHandle thePicture;
- short count, i;
- ElemPtr elem;
-
- Doc_CalcSelectionBounds( theDoc, &boundsRect );
- if (EmptyRect( &boundsRect ))
- return nil;
-
- // move the coordinate system according to the scroll bars.
- GetOrigin( &saveOrigin );
- Doc_SetGrafOrigin( theDoc );
-
- thePicture = OpenPicture( &boundsRect );
-
- // loop through all the items, drawing the selected ones.
-
- count = ElemColl_GetCount (theDoc->contentColl);
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem))
- {
- Elem_Draw (elem, false);
- }
- }
-
- ClosePicture();
-
- // set the coordinate system back.
- SetOrigin( saveOrigin.h, saveOrigin.v );
-
- return thePicture;
- }
-
- //---------------------------------------------------------------------------
- // save the current state of the document in undoData.
-
- void Doc_SaveUndo(
- DocPtr theDoc )
- {
- #pragma unused (theDoc)
-
- #if 0
- OSErr theErr;
-
- // if the flag was set to save on the next operation...
- if (theDoc->bUndoNext)
- {
- if (undoData != nil)
- {
- DisposeHandle( undoData );
- undoData = nil;
- }
-
- undoData = (**theDoc->TEH).hText;
- theErr = HandToHand( &undoData );
- if (theErr != noErr)
- undoData = nil; // give up on the undo.
-
- theDoc->undoSelStart = (**theDoc->TEH).selStart;
- theDoc->undoSelEnd = (**theDoc->TEH).selEnd;
- }
- #endif
- }
-
- //---------------------------------------------------------------------------
- // restore the state of the document from undoData.
-
- void Doc_RestoreUndo(
- DocPtr theDoc )
- {
- #pragma unused (theDoc)
-
- #if 0
- Handle theHandle;
- OSErr theErr;
- short selStartTemp, selEndTemp;
-
- if (undoData != nil)
- {
- // set the text
- theHandle = (**theDoc->TEH).hText;
- theErr = HandToHand( &theHandle ); // save the current text
- if (theErr != noErr)
- undoData = nil;
- else
- {
- // save the current selection
- selStartTemp = (**theDoc->TEH).selStart;
- selEndTemp = (**theDoc->TEH).selEnd;
-
- // restore the undo text
- doSetText( undoData, theDoc->TEH, theDoc );
- doSetSelect( theDoc->undoSelStart, theDoc->undoSelEnd, theDoc->TEH, false, theDoc );
-
- theDoc->undoSelStart = selStartTemp;
- theDoc->undoSelEnd = selEndTemp;
- }
-
- // store the current text into the undo text
- undoData = theHandle;
- }
- #endif
- }
-
-
- //---------------------------------------------------------------------------
- // dispose any memory used by undoData.
-
- void Doc_DisposeUndo(
- DocPtr theDoc )
- {
- #pragma unused (theDoc)
-
- #if 0
- if (undoData != nil)
- {
- DisposeHandle( undoData );
- undoData = nil;
- }
- #endif
- }
-
- //---------------------------------------------------------------------------
- // return the window that owns this document.
-
- WindowPtr Doc_GetWindow(
- DocPtr theDoc )
- {
- WindowPtr curWindow;
-
- curWindow = FrontWindow();
- while ((curWindow != nil) && ((DocPtr)GetWRefCon( curWindow ) != theDoc))
- curWindow = (WindowPtr)(((WindowPeek)curWindow)->nextWindow);
-
- return curWindow;
- }
-
- //---------------------------------------------------------------------------
-
- void Doc_Draw(
- DocPtr theDoc )
- {
- Rect theRect;
- WindowPtr theWindow;
- long i;
- short count;
- ElemPtr elem;
-
- theWindow = Doc_GetWindow( theDoc );
-
- count = ElemColl_GetCount (theDoc->contentColl);
-
- // draw the content area here. Back to front
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- Elem_GetDisplayRect (elem, &theRect);
-
- if (RectInRgn( &theRect, theWindow->clipRgn ))
- Elem_Draw (elem, true);
- }
-
- }
-
- //---------------------------------------------------------------------------
- // read theWindow's document data from the existing file theDoc->fileRefNum.
-
- OSErr Doc_ReadFile(
- DocPtr theDoc )
- {
- Ptr bufPtr;
- OSErr theErr;
- fileHeader theFileHeader;
- WindowPtr theWindow;
- long length, i;
- IOStream ioStream;
- ElemPtr elem;
-
- ioStream.type = fileStream;
- ioStream.theDoc = theDoc;
- ioStream.fileRef = theDoc->fileRefNum;
-
- theErr = noErr;
- bSetCursorWatch();
-
- theErr = SetFPos( theDoc->fileRefNum, fsFromStart, 0L );
-
- #ifdef USE_CALIB
-
- if (gCALibExists)
- {
- FInfo finderInfo;
-
- FSpGetFInfo( &(theDoc->fileSpec), &finderInfo );
- if (finderInfo.fdType == kContainerFileType)
- {
- // For doc's stored using the Bento Model, we need to
- // open now. Close the file so CAOpenDocument forsure can read it.
- theErr = FSClose( theDoc->fileRefNum );
- theDoc->storageModel = kBentoStorageModel;
-
- // The CADocument's current kind is set to the first content value
- // by default, stream offset == 0 also by default.
- theDoc->partDocRef = CAOpenDocument( &(theDoc->fileSpec), NULL, 0, 0);
- }
- else
- {
- theDoc->storageModel = kNativeStorageModel;
- }
- }
-
- #endif
-
- if (theErr == noErr)
- {
- // read in the header.
- length = sizeof(fileHeader);
- bufPtr = (Ptr)&theFileHeader;
-
- #ifdef USE_CALIB
- if (gCALibExists)
- theErr = CAUtil_ReadWrapper (&ioStream, bufPtr, &length);
- else
- theErr = FSRead( theDoc->fileRefNum, &length, bufPtr );
- #else
- theErr = FSRead( theDoc->fileRefNum, &length, bufPtr );
- #endif
-
- }
-
- if (theErr == noErr)
- {
- if (theFileHeader.version != kFileVersion)
- theErr = paramErr;
- }
-
- if (theErr == noErr)
- {
- // get the header data
- theDoc->hPrint = (THPrint)NewHandle( (long)sizeof(TPrint) );
- theErr = MemError();
- }
-
- if (theErr == noErr)
- {
- BlockMoveData( &theFileHeader.printRecord, *(theDoc->hPrint), (long)sizeof(TPrint) );
-
- // 8 by 10 inches at 72dpi
- PrOpen();
- SetRect( &theDoc->documentRect, 0, 0, 8*72, 10*72 );
- if (PrError() == noErr)
- if (!checkPrintHandle( &theDoc->hPrint ))
- // the printable area of the paper
- theDoc->documentRect = (**theDoc->hPrint).prInfo.rPage;
-
- PrClose();
- }
-
- theWindow = Doc_GetWindow( theDoc );
-
- if (theWindow)
- {
- MoveWindow(
- theWindow, theFileHeader.windowRect.left,
- theFileHeader.windowRect.top, false );
- SizeWindow(
- theWindow, theFileHeader.windowRect.right - theFileHeader.windowRect.left,
- theFileHeader.windowRect.bottom - theFileHeader.windowRect.top, true );
-
- theDoc->documentRect = theFileHeader.documentRect;
-
- // make sure this window is visible on screen
- Win_CheckWindowPosition( theWindow );
-
- SetControlValue( theDoc->hScrollBar, theFileHeader.hScrollValue );
- SetControlValue( theDoc->vScrollBar, theFileHeader.vScrollValue );
-
- theDoc->insertionPoint = theFileHeader.insertionPoint;
-
- }
-
- // read in the itemArray.
- if (theErr == noErr)
- {
-
- for (i=0; i<theFileHeader.elemCount; ++i)
- {
- elem = Elem_Read(&ioStream);
- ElemColl_AddElem (theDoc->contentColl, elem);
- }
-
- }
-
- #ifdef USE_CALIB
-
- if (gCALibExists)
- {
-
- if (theDoc->storageModel == kNativeStorageModel)
- {
- long offset;
- CASize length;
- long numBytes;
-
- // $$$$$ need to perform error handling here…
- offset = 0L;
- numBytes = sizeof(length);
- theErr = FSRead( theDoc->fileRefNum, &numBytes, (Ptr)&length );
-
- theErr = GetFPos( theDoc->fileRefNum, &offset);
- theErr = FSClose( theDoc->fileRefNum);
-
- theDoc->partDocRef = CAOpenDocument( &(theDoc->fileSpec),
- NULL,
- offset,
- length);
-
- if (CAError())
- ; // handle the error
-
- FSpOpenDF( &theDoc->fileSpec, fsRdWrPerm, &theDoc->fileRefNum );
- }
- else
- {
- // Get back a refNum so other stuff doesn't break;
- theErr = FSpOpenDF( &theDoc->fileSpec, fsRdPerm, &theDoc->fileRefNum );
- }
-
- }
- #endif
-
- SetCursor( &qd.arrow );
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // write the document's data to the existing file theDoc->fileRefNum.
-
- OSErr Doc_WriteFile(
- DocPtr theDoc )
- {
- fileHeader theFileHeader;
- Ptr bufPtr;
- OSErr theErr = noErr;
- WindowPtr theWindow;
- long length, i;
- IOStream ioStream;
- short count;
- ElemPtr elem;
-
- ioStream.type = fileStream;
- ioStream.theDoc = theDoc;
- ioStream.fileRef = theDoc->fileRefNum;
-
- #ifdef USE_CALIB
-
- if (!gCALibExists)
- theErr = SetFPos( theDoc->fileRefNum, fsFromStart, 0L );
- else
- {
- if (theDoc->storageModel == kNativeStorageModel)
- {
- theErr = SetFPos( theDoc->fileRefNum, fsFromStart, 0L );
- }
- else
- {
- // Set the Bento stream offset back to 0
- CASetDocumentKind (theDoc->partDocRef, kCASampleKind);
- CASetOffset( theDoc->partDocRef, 0 );
- }
- }
- #else
-
- theErr = SetFPos( theDoc->fileRefNum, fsFromStart, 0L );
- #endif
-
- if (theErr != noErr)
- return theErr;
-
- bSetCursorWatch();
-
- // fill in the header.
- theFileHeader.version = kFileVersion;
- if (theDoc->hPrint != nil)
- BlockMoveData( *(theDoc->hPrint), &theFileHeader.printRecord, (long)sizeof(TPrint) );
- else
- blockErase( (Ptr)&theFileHeader.printRecord, (long)sizeof(TPrint) );
-
- theWindow = Doc_GetWindow( theDoc );
- if (theWindow != nil)
- {
- GrafPtr savePort;
-
- GetPort (&savePort);
- SetPort (theWindow);
-
- theFileHeader.windowRect = theWindow->portRect;
- RectLocalToGlobal( &theFileHeader.windowRect );
-
- SetPort (savePort);
-
- }
- else
- SetRect( &theFileHeader.windowRect, 20, 20, 300, 400 );
-
- theFileHeader.documentRect = theDoc->documentRect;
- theFileHeader.hScrollValue = GetControlValue( theDoc->hScrollBar );
- theFileHeader.vScrollValue = GetControlValue( theDoc->vScrollBar );
- theFileHeader.elemCount = ElemColl_GetCount (theDoc->contentColl);;
- theFileHeader.insertionPoint = theDoc->insertionPoint;
-
- length = sizeof(fileHeader);
- bufPtr = (Ptr)&theFileHeader;
-
- #ifdef USE_CALIB
- if (gCALibExists)
- theErr = CAUtil_WriteWrapper (&ioStream, bufPtr, &length);
- else
- theErr = FSWrite( theDoc->fileRefNum, &length, bufPtr );
- #else
-
- theErr = FSWrite( theDoc->fileRefNum, &length, bufPtr );
- #endif
-
- // write out the contents.
- if (theErr == noErr)
- {
- count = ElemColl_GetCount (theDoc->contentColl);
- for (i=1; i <= count; i++)
- {
-
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- Elem_Write (elem, &ioStream);
-
- }
- }
-
- #ifdef USE_CALIB
-
-
- if (gCALibExists)
- {
- long offset;
- CASize length;
- long numBytes;
-
- // write out each embedded frame's data
-
- if (theDoc->storageModel == kNativeStorageModel)
- {
- // Make room for the Container length
- offset = 0L;
- numBytes = sizeof(length);
- theErr = FSWrite (theDoc->fileRefNum, &numBytes, (Ptr) &length);
- theErr = GetFPos(theDoc->fileRefNum, &offset);
-
- theErr = FSClose (theDoc->fileRefNum);
- CASaveDocument( theDoc->partDocRef, &(theDoc->fileSpec), NULL , offset, &length);
- if (CAError())
- ; // handle the error
- theErr = FSpOpenDF( &theDoc->fileSpec, fsRdWrPerm, &theDoc->fileRefNum );
-
- // Write out the correct length now
- theErr = SetFPos( theDoc->fileRefNum, fsFromStart, offset - 4 );
- numBytes = sizeof(length);
- theErr = FSWrite( theDoc->fileRefNum, &numBytes, (Ptr) &length );
- theErr = SetFPos( theDoc->fileRefNum, fsFromLEOF, 0 );
- }
- else
- {
- CASize dataLength;
-
- // Close the file so CASaveDocument can write to it
- if (theDoc->fileRefNum != 0)
- theErr = FSClose( theDoc->fileRefNum );
-
- dataLength = 0L;
- //CASaveDocument( theDoc->partDocRef, 0, &dataLength, &(theDoc->fileSpec), NULL );
-
- // save the container document in place
- CASaveDocument( theDoc->partDocRef, &(theDoc->fileSpec), NULL, 0, &dataLength);
- if (CAError())
- ; // handle the error
-
-
- // Get back a refNum so other stuff doesn't break
- theErr = FSpOpenDF( &(theDoc->fileSpec), fsRdPerm, &theDoc->fileRefNum );
- }
- }
- #endif
-
- // set the end of the file (in case it was larger).
- //if (theErr = SetEOF(theDoc->fileRefNum, sizeof(fileHeader)+length))
- // return(theErr);
- SetCursor( &qd.arrow );
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // Save theWindow's data in a new file after asking the user to name the file.
-
- OSErr Doc_SaveAs(
- DocPtr theDoc )
- {
- Str255 thePrompt, theName;
- StandardFileReply theReply;
- OSErr theErr;
- Point thePt;
- WindowPtr theWindow;
-
- theErr = noErr;
-
- theWindow = Doc_GetWindow( theDoc );
- if (theWindow != nil)
- GetWTitle( theWindow, theName );
- else
- theName[0] = '\0';
-
- #ifdef USE_CALIB
-
- if (gCALibExists)
- {
- short storageModel;
-
- // CASample will pose a custom dialog which has a radio button
- // to specify Native or Bento storage.
- thePt.h = thePt.v = -1;
- storageModel = theDoc->storageModel;
- GetIndString( thePrompt, kStrings, itSavePrompt );
-
- if (!CARequestModalFocus( theWindow))
- return kCASErrCALib;
-
- CustomPutFile(
- thePrompt, theName, &theReply, kCASaveFileDialog, thePt, gDlgHookYDUPP,
- gWindowEventFilterYDUPP, nil, nil, (short*) &storageModel );
-
- CARelinquishModalFocus( theWindow );
-
- theDoc->storageModel = storageModel;
- }
- else
- {
- // ask the user to name the file.
- // {-1, -1} centers PutFile on the screen.
- thePt.h = thePt.v = -1;
- GetIndString( thePrompt, kStrings, itSavePrompt );
-
- CustomPutFile(
- thePrompt, theName, &theReply, 0, thePt, nil,
- gWindowEventFilterYDUPP, nil, nil, nil );
-
- }
- #else
-
- // ask the user to name the file.
- // {-1, -1} centers PutFile on the screen.
- thePt.h = thePt.v = -1;
- GetIndString( thePrompt, kStrings, itSavePrompt );
- CustomPutFile(
- thePrompt, theName, &theReply, 0, thePt, nil,
- gWindowEventFilterYDUPP, nil, nil, nil );
- #endif
-
- // if the reply was not good, the user canceled.
- if (!theReply.sfGood)
- return userCanceledErr;
-
- // if we're not replacing an existing file, create a new file.
- if (!theReply.sfReplacing)
- {
- theErr =
- FSpCreate(
- &theReply.sfFile, kOurFileCreator,
- kOurFileType, theReply.sfScript );
- }
-
- theDoc->fileSpec = theReply.sfFile;
-
- // if the document was open in another file, close it.
- if (theErr == noErr)
- if (theDoc->fileRefNum != 0)
- theErr = FSClose( theDoc->fileRefNum );
-
- // open the new file.
- if (theErr == noErr)
- theErr = FSpOpenDF( &theReply.sfFile, fsRdWrPerm, &theDoc->fileRefNum );
-
- // write the doc data.
- if (theErr == noErr)
- theErr = Doc_WriteFile( theDoc );
-
- #ifdef USE_CALIB
- if (gCALibExists)
- {
- // Change the file type appropriatly
- FInfo finderInfo;
- OSType fileType;
-
- fileType = (theDoc->storageModel == kBentoStorageModel) ? kContainerFileType
- : kOurFileType;
- FSpGetFInfo( &theReply.sfFile, &finderInfo );
- finderInfo.fdType = fileType;
- FSpSetFInfo( &theReply.sfFile, &finderInfo );
-
- }
- #endif
-
- if (theErr != noErr)
- DEBUGSTR( "\pDoc_SaveAs: we have an error" );
-
- // change the window title and clear the changed flag.
- if (theErr == noErr)
- {
- if (theWindow != nil)
- SetWTitle( theWindow, theReply.sfFile.name );
- Doc_SetDirty( theDoc, false );
- }
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // Save theWindow's data in a file asking the user to name the file if necessary.
-
- OSErr Doc_Save(
- DocPtr theDoc )
- {
- OSErr theErr;
-
- // if the file has no refNum, then we need to name it
- theErr = noErr;
- if (theDoc->fileRefNum == 0)
- theErr = Doc_SaveAs( theDoc );
- else
- {
- theErr = Doc_WriteFile( theDoc );
- if (theErr == noErr)
- Doc_SetDirty( theDoc, false );
- }
-
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // Doc_New - does any initialization that the doc and it's window might need.
-
- OSErr Doc_New( void )
- {
- DocPtr theDoc;
- GrafPtr savePort;
- WindowPtr theWindow;
- Rect windowRect;
- OSErr theErr;
- FSSpec fileSpec;
-
- theErr = noErr;
-
- theDoc = (DocPtr)NewPtrClear( (long)sizeof(DocRecord) );
- if (theDoc != nil)
- Doc_InitData( theDoc );
- else
- {
- theErr = MemError();
- return theErr;
- }
-
- // create a new window
- SetRect( &windowRect, 10, GetMBarHeight() + 22, 300, 400 );
- OffsetRect( &windowRect, gWindowCount * 10, gWindowCount * 10 );
-
- GetPort( &savePort );
-
- // if the window failed to be created, get rid of the theDoc and return an error.
- theWindow = Win_New( nil, &windowRect );
- if (theWindow == nil)
- {
- DisposePtr( (Ptr)theDoc );
- return memFullErr;
- }
-
- // attach the theDoc to our window.
- SetWRefCon( theWindow, (long)theDoc );
-
- #ifdef USE_CALIB
- sCopyStr ("\pUntitled", fileSpec.name);
- FindFolder(0, kDesktopFolderType, kDontCreateFolder,&(fileSpec.vRefNum),&(fileSpec.parID));
- MakeDatedUniqueFSSpec (&fileSpec);
- theDoc->fileSpec = fileSpec;
- SetWTitle( theWindow, fileSpec.name );
-
- #else
- // set the window title.
- gWindowCount += 1;
- NumToString( gWindowCount, theStr );
- GetIndString( theName, kStrings, itUntitled );
- sParamStr( theName, theStr, nil, nil, nil, nil, nil, nil, nil, nil, nil );
- SetWTitle( theWindow, theName );
- #endif
-
-
- SetPort( theWindow );
-
- // check the print handle and get the size of a page.
- // 8 by 10 inches at 72dpi
- SetRect( &theDoc->documentRect, 0, 0, 8*72, 10*72 );
- PrOpen();
- theErr = PrError();
- if (theErr == noErr)
- {
- if (theDoc->hPrint != nil)
- if (checkPrintHandle( &theDoc->hPrint ))
- // the printable area of the paper
- theDoc->documentRect = (**theDoc->hPrint).prInfo.rPage;
- PrClose();
- }
-
- // set up the scroll bars
- // (hScrollBarRect and vScrollBarRect have not been set yet, but it's OK).
- theDoc->hScrollBar =
- NewControl(
- theWindow, &theDoc->hScrollBarRect, "\p", true,
- 0, 0, 0, scrollBarProc, (long)theDoc );
- theDoc->vScrollBar =
- NewControl(
- theWindow, &theDoc->vScrollBarRect, "\p", true,
- 0, 0, 0, scrollBarProc, (long)theDoc );
-
- #ifdef USE_CALIB
- if (gCALibExists)
- theErr = Doc_InstallCALibHandlers( theDoc, theWindow, true );
- #endif
-
- // adjust the window rects and regions and the scroll bars.
- // adjust the size for zooming.
- Win_Adjust( theWindow );
- Win_AdjustStdState( theWindow );
-
- // now show the window.
- Win_ShowHide( theWindow, true );
- App_SetFrontDocWindow( theWindow );
-
- SetPort( savePort );
-
- #ifdef USE_CALIB
- // install the drag handlers
- if (gCALibExists)
- theErr = Doc_InstallDragHandlers( theDoc, theWindow );
- #endif
-
- App_LogOpenDocument (theDoc);
-
- return theErr;
- }
-
-
- //---------------------------------------------------------------------------
- // Doc_Open - does any initialization that the doc and its window might need.
-
- OSErr Doc_Open(
- FSSpec *theSpec )
- {
- GrafPtr savePort;
- WindowPtr theWindow;
- OSErr theErr;
- DocPtr theDoc;
- Rect windowRect;
- short i;
- ElemPtr elem;
- short count;
-
- theDoc = (DocPtr)NewPtrClear( (long)sizeof(DocRecord) );
- if (theDoc != nil)
- Doc_InitData( theDoc );
- else
- {
- theErr = MemError();
- return theErr;
- }
-
- theDoc->fileSpec = *theSpec;
-
-
- // open the data fork
- theErr = FSpOpenDF( theSpec, fsRdWrPerm, &theDoc->fileRefNum );
- if (theErr == noErr)
- {
- theDoc->fileLocked = false;
- }
- else if (theErr == permErr)
- {
- // open the data fork (read-only)
- theDoc->fileLocked = true;
- theErr = FSpOpenDF( theSpec, fsRdPerm, &theDoc->fileRefNum );
- }
- else
- {
- DisposePtr( (Ptr)theDoc );
- return theErr;
- }
-
- SetRect( &windowRect, 10, 10, 200, 200 );
- theWindow = Win_New( nil, &windowRect );
- if (theWindow == nil)
- {
- FSClose( theDoc->fileRefNum );
- DisposePtr( (Ptr)theDoc );
- return memFullErr;
- }
-
- // attach the theDoc to our window.
- SetWRefCon( theWindow, (long)theDoc );
-
- // set the window's title
- SetWTitle( theWindow, theSpec->name );
-
- GetPort( &savePort );
- SetPort( (GrafPtr)theWindow );
-
- // set up the scroll bars
- // (hScrollBarRect and vScrollBarRect have not been set yet, but it's OK).
- theDoc->hScrollBar =
- NewControl(
- theWindow, &theDoc->hScrollBarRect, "\p", true,
- 0, 0, 0, scrollBarProc, (long)theDoc );
- theDoc->vScrollBar =
- NewControl(
- theWindow, &theDoc->vScrollBarRect, "\p", true,
- 0, 0, 0, scrollBarProc, (long)theDoc );
-
- // read in the file data.
- // $$$$$ don't forget to deal with errors
- theErr = Doc_ReadFile( theDoc );
- if (theErr != noErr)
- {
- FSClose( theDoc->fileRefNum );
- DisposePtr( (Ptr)theDoc );
- SetPort( savePort );
- return theErr;
- }
-
- #ifdef USE_CALIB
- if (gCALibExists)
- theErr = Doc_InstallCALibHandlers( theDoc, theWindow, false );
- #endif
-
- // adjust the window rects and regions and the scroll bars.
- // adjust the size for zooming.
- Win_Adjust( theWindow );
- Win_AdjustStdState( theWindow );
-
- SetPort( savePort );
-
- #ifdef USE_CALIB
-
- // install the drag handlers
- theErr = Doc_InstallDragHandlers( theDoc, theWindow );
-
- // Make all content elements visible
- count = ElemColl_GetCount (theDoc->contentColl);
-
- for (i=1; i<=count; ++i)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- Elem_SetVisible (elem, true);
- }
-
- #endif
-
- App_LogOpenDocument (theDoc);
-
-
- return noErr;
- }
-
- //---------------------------------------------------------------------------
- // Doc_Close - Frees the doc item list
-
- void Doc_Close(
- DocPtr theDoc )
- {
-
- ElemColl_FreeAll (theDoc->contentColl);
-
- #ifdef USE_CALIB
- if (gCALibExists)
- CACloseDocument( (CADocumentRef)theDoc->partDocRef );
- #endif
-
-
- }
-
-
- //---------------------------------------------------------------------------
- // Doc_InstallDragHandlers - install the drag handlers.
-
- static OSErr Doc_InstallDragHandlers(
- DocPtr theDoc,
- WindowPtr theWindow )
- {
- OSErr theErr;
-
- theErr = noErr;
-
- #ifdef USE_CALIB
-
- if (gCALibExists)
- {
- // install the drag handlers on the window.
- if (hasDragMgr)
- {
- theErr = CAInstallTrackingHandler( gDragTrackingHandlerUPP,
- theWindow,
- nil );
-
- theErr = CAInstallReceiveHandler( gDragReceiveHandlerUPP,
- theWindow,
- nil );
- }
- }
- else
- {
- // install the drag handlers on the window.
- if (hasDragMgr)
- {
- theErr =
- InstallTrackingHandler(
- gDragTrackingHandlerUPP,
- theWindow,
- nil );
- if (theErr == noErr)
- {
- theErr =
- InstallReceiveHandler(
- gDragReceiveHandlerUPP,
- theWindow,
- nil );
- if (theErr != noErr)
- RemoveTrackingHandler(
- gDragTrackingHandlerUPP,
- theWindow );
- }
- }
- }
-
- #else
-
- // install the drag handlers on the window.
- if (hasDragMgr)
- {
- theErr =
- InstallTrackingHandler(
- gDragTrackingHandlerUPP,
- theWindow,
- nil );
- if (theErr == noErr)
- {
- theErr =
- InstallReceiveHandler(
- gDragReceiveHandlerUPP,
- theWindow,
- nil );
- if (theErr != noErr)
- RemoveTrackingHandler(
- gDragTrackingHandlerUPP,
- theWindow );
- }
- }
- #endif
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // Doc_InstallCALibHandlers - install the standard CALib callback handlers.
-
- static OSErr Doc_InstallCALibHandlers(
- DocPtr theDoc,
- WindowPtr theWindow,
- Boolean isNewDoc )
- {
- CADocumentRef newDocRef;
- OSErr theErr;
-
- // partDocRef was set in Doc_ReadFile
- theErr = noErr;
-
- if (isNewDoc)
- {
- // Don't bind CADocument until save time
- newDocRef = CACreateDocument(NULL, NULL);
- theDoc->partDocRef = newDocRef;
- theDoc->storageModel = kNativeStorageModel;
-
- }
- else
- newDocRef = theDoc->partDocRef;
-
- CAInstallFocusNotification(
- App_CAFocusAcquiredProc, App_CAFocusLostProc, newDocRef );
- if (theErr = CAError())
- ; // handle the error
-
- CARegisterRootWindow( theWindow, newDocRef);
- if (theErr = CAError())
- ; // handle the error
-
- CAInstallWindowActivateHandler( App_CAWindowActivateHandler, newDocRef );
- if (theErr = CAError())
- ; // handle the error
-
- CAInstallFrameShapeRequestHandler( App_CAFrameShapeRequestHandler, newDocRef );
- if (theErr = CAError())
- ; // handle the error
-
-
- CAInstallBorderAdjuster( newDocRef, App_CAAdjustBorderHandler );
- if (theErr = CAError())
- ; // handle the error
-
- return theErr;
- }
-
- //---------------------------------------------------------------------------
- // Edit Menu Items
- //---------------------------------------------------------------------------
- // Doc_UndoMenu - undo the last operation.
- // assume it's our window.
-
- void Doc_UndoMenu(
- DocPtr theDoc )
- {
- // if there is no undoText or the file is locked, get out.
- if ((theDoc == nil) || (theDoc->undoData == nil) || theDoc->fileLocked)
- return;
-
- Doc_RestoreUndo( theDoc );
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- }
-
-
- #ifdef USE_CALIB
- //---------------------------------------------------------------------------
- // odcCutMenu - CALib version
-
- void Doc_CutMenu(
- DocPtr theDoc )
- {
- PicHandle thePicture;
-
- Doc_CopyMenu(theDoc, true);
-
- // delete the selection.
- Doc_DeleteCurrentSelection( theDoc );
-
- }
-
- //---------------------------------------------------------------------------
- // odcCutMenu - cut the selection from the document onto the clipboard.
- // Assume it's our window and there is a selection.
-
- #else
-
- void Doc_CutMenu(
- DocPtr theDoc )
- {
- OSErr theErr;
-
- // if the file is locked, beep and get out.
- // $$$$$ the cut menu should be disabled if the file is locked, so
- // we should never get here.
- if (theDoc->fileLocked)
- {
- SysBeep(2);
- return;
- }
-
- Doc_SaveUndo( theDoc );
- Doc_SetDirty( theDoc, true );
-
- // create a picture from the selected items.
- thePicture = Doc_CreateSelectionPicture( theDoc );
- if (thePicture == nil)
- return;
-
- // clear the scrap.
- ZeroScrap();
-
- // put our picture on the scrap ($$$$$ ignore any errors?).
- HLock( (Handle)thePicture );
- theErr = PutScrap( GetHandleSize( (Handle)thePicture ), kPICTType, (Ptr)*thePicture ); // put resource on clipboard
-
- // get rid of the picture.
- KillPicture( thePicture );
-
- // delete the selection.
- Doc_DeleteCurrentSelection( theDoc );
-
- // make sure the selection is visible in the window.
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- }
- #endif
-
-
-
- #if CALIB_SAVVY_DATA_TRANSFER
-
- //---------------------------------------------------------------------------
- // Doc_CopyMenu - CALib savvy version
- //
- // CA writes all scrap formats to the system scrap as usual.
- // In order to faciltate transfer of a single embedded frame to other OpenDoc
- // documents the CA must add a scrap type of 'odcn' if only a single frame is selected.
-
- void Doc_CopyMenu(
- DocPtr theDoc, Boolean isCut )
- {
- PicHandle pictHandle;
- Handle scrapHandle;
- CASize length;
- CADocumentRef clipDoc = NULL;
- Handle compositeHandle;
- Ptr bufferPtr;
- long dataLength;
- OSErr theErr;
- Boolean anyFramesSelected;
- Handle containerHandle = NULL;
- Boolean singleFrame = false;
-
- // Empty the system scrap
- ZeroScrap();
-
- anyFramesSelected = (Doc_GetSelectedFrameCount (theDoc) > 0);
-
- // Get the CADocumentRef representing the OD Clipboard object
-
- containerHandle = NewHandle(0);
-
- if (anyFramesSelected)
- {
- clipDoc = CAGetClipboardDocument ();
- }
-
- // Create internal data stream "kCASScrapType".
- scrapHandle = Doc_CreateCAScrapFromSelection( theDoc, clipDoc, isCut ? kCACloneCut : kCACloneCopy );
-
- if (anyFramesSelected)
- {
-
- // Save the clipboard doc container into containerHandle
- CASaveDocument (clipDoc, NULL, &containerHandle, 0, &length);
- CACloseDocument (clipDoc);
-
- }
-
- if (scrapHandle == NULL)
- {
- // Single frame case
- singleFrame = true;
-
- // Write the transferDoc to the system scrap as kCAScrapTypeODBentoContainer
- HLock (containerHandle);
-
- // Write the transferDoc container to
- theErr = PutScrap(
- GetHandleSize (containerHandle),
- kCAScrapTypeODBentoContainer, (Ptr)*containerHandle ); // put resource on clipboard
-
- HUnlock (containerHandle);
- DisposeHandle (containerHandle);
-
- }
- else
- {
-
- // Build the composite data stream (length - scrap data - length - doc data)
-
- compositeHandle = NewHandle ( (GetHandleSize(scrapHandle) + GetHandleSize (containerHandle)
- + sizeof (long) + sizeof (long)) );
-
- HLock( (Handle)compositeHandle );
-
- bufferPtr = *compositeHandle;
-
- // Write the scrap data length
- dataLength = GetHandleSize(scrapHandle);
- BlockMove (&dataLength, bufferPtr, sizeof (long));
- bufferPtr = (Ptr) ((long) bufferPtr + sizeof (long));
-
- HLock( (Handle)scrapHandle );
-
- if (dataLength > 0)
- {
- // Write the scrap data
- BlockMove (*scrapHandle, bufferPtr, dataLength);
- bufferPtr = (Ptr) ((long) bufferPtr + dataLength);
- }
-
- HUnlock ((Handle) scrapHandle);
- DisposeHandle (scrapHandle);
-
-
- // Write the doc data length
- HLock( (Handle)containerHandle );
- dataLength = GetHandleSize(containerHandle);
- BlockMove (&dataLength, bufferPtr, sizeof (long));
- bufferPtr = (Ptr) ((long) bufferPtr + sizeof (long));
-
-
- if (dataLength > 0)
- {
- // Write the doc data
- BlockMove (*containerHandle, bufferPtr, dataLength);
- bufferPtr = (Ptr) ((long) bufferPtr + dataLength);
- }
-
- HUnlock ((Handle) containerHandle);
- DisposeHandle (containerHandle);
-
- theErr = PutScrap(
- GetHandleSize (compositeHandle),
- kCACASScrapType, (Ptr)*compositeHandle ); // put resource on clipboard
-
- if (theErr)
- Error_ReportOperationError (kCASErrSystemScrapIO);
-
- HUnlock( (Handle)compositeHandle );
- DisposeHandle( compositeHandle );
- }
-
- //
- // write PICT flavor
- //
-
- pictHandle = Doc_CreateSelectionPicture (theDoc);
- length = GetHandleSize ((Handle)pictHandle);
- HLock ((Handle) pictHandle);
-
- theErr = PutScrap(
- GetHandleSize ((Handle)pictHandle),
- kPICTType, (Ptr)*pictHandle ); // put resource on clipboard
- HUnlock ((Handle) pictHandle);
- KillPicture (pictHandle);
-
- // make sure the selection is visible in the window.
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- }
-
- #else // CALIB_FRIENDLY_DATA_TRANSFER
-
- //---------------------------------------------------------------------------
- // Doc_CopyMenu - CALib Friendly version
- //
- // CA writes all scrap formats to the CALib clipboard document.
- // When the clipboard document is closed a scrap type of 'odcn' is added to the
- // system scrap. This allow other OpenDoc parts to read it.
- // In order to facilate transfer to other non-OpenDoc application, CAExportClipboard()
- // must be called on a suspend event.
-
- void Doc_CopyMenu(
- DocPtr theDoc, Boolean isCut )
- {
- Handle scrapHandle;
- CASize length;
- CADocumentRef clipDoc;
- PicHandle pictHandle;
- char pictTypeStr[256];
-
- #ifdef USE_CALIB
- {
-
- // Get the CADocumentRef representing the OD Clipboard object
- clipDoc = CAGetClipboardDocument();
-
- // Create internal data stream "kCASScrapType".
- scrapHandle = Doc_CreateCAScrapFromSelection( theDoc, clipDoc, isCut ? kCACloneCut : kCACloneCopy );
-
- CAAddKind(clipDoc, kCACASScrapTypeStr);
-
- }
- #else
-
- // Copy our own format "kCASScrapType" data to the scrap
- scrapHandle = Doc_CreateScrapFromSelection( theDoc );
-
- #endif
-
- // Note: If scrapHandle == nil then we do nothing. A scrap type of
- // kODScrapTypeODBentoContainer is added to the system scrap by default.
-
- if (scrapHandle != nil)
- {
- HLock( (Handle)scrapHandle );
-
- #ifdef USE_CALIB
-
- length = GetHandleSize (scrapHandle);
- CAWrite (clipDoc, (char*)*scrapHandle, &length);
-
- #else
- theErr =
- PutScrap(
- GetHandleSize (scrapHandle),
- kCASScrapType, (Ptr)*scrapHandle ); // put resource on clipboard
-
- #endif
-
- DisposeHandle( scrapHandle );
- }
-
- // write PICT flavor
- pictHandle = Doc_CreateSelectionPicture (theDoc);
- length = GetHandleSize ((Handle)pictHandle);
- HLock ((Handle) pictHandle);
-
- #ifdef USE_CALIB
- strcpy (pictTypeStr, kODAppleScrapTypePrefix);
- strcat (pictTypeStr, "PICT");
- CAAddKind (clipDoc, pictTypeStr);
- CAWrite (clipDoc, (char*)*pictHandle, &length);
- CACloseDocument (clipDoc);
- #else
-
- theErr = PutScrap(GetHandleSize (pictHandle),
- kPICTType, (Ptr)*pictHandle ); // put resource on clipboard
-
- #endif
-
- HUnlock ((Handle) pictHandle);
- KillPicture (pictHandle);
-
- // make sure the selection is visible in the window.
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- }
-
- #endif
-
- #if CALIB_SAVVY_DATA_TRANSFER
-
- //---------------------------------------------------------------------------
- // Doc_PasteMenu - CALib friendly version
- //
- // Examine the system scrap for types available. A type of 'odcn' indicates
- // a part on the clipboard
-
- void Doc_PasteMenu(
- DocPtr theDoc )
- {
- PicHandle thePicture;
- Handle scrapHandle;
- long scrapLength, scrapOffset;
- long scrapOff = 0;
- ElemPtr elem;
- Handle compositeHandle;
- Handle docHandle;
- long length;
-
- // if the file is locked, beep and get out.
- // $$$$$ the paste menu should be disabled if the file is locked, so
- // we should never get here.
- if (theDoc->fileLocked)
- {
- SysBeep(2);
- return;
- }
-
- // First check for our internal scrap format
- if (CAUtil_ScrapHasType (kCACASScrapType, &scrapOffset, &length))
- {
- CADocumentRef transferDoc = NULL;
- Ptr bufferPtr;
- long docOffset;
-
- compositeHandle = NewHandle( 0L );
- length = GetScrap( compositeHandle, kCACASScrapType, &scrapOffset );
-
- HLock (compositeHandle);
-
- bufferPtr = *compositeHandle;
-
- // Read the scrap data length
- BlockMove (bufferPtr, &scrapLength, sizeof (long));
- bufferPtr = (Ptr) ((long) bufferPtr + sizeof (long));
-
- // Read the scrap data
- scrapHandle = NewHandle (scrapLength);
- HLock (scrapHandle);
- BlockMove (bufferPtr, *scrapHandle, scrapLength);
- HUnlock (scrapHandle);
- bufferPtr = (Ptr) ((long) bufferPtr + scrapLength);
-
- // Read the CADocument length
- BlockMove (bufferPtr, &length, sizeof (long));
-
- if (length > 0)
- {
-
- // Compute the CADocument stream offset
- docOffset = sizeof (long) + scrapLength + sizeof (long);
-
- // open the CADocument from the handle
- transferDoc = CAOpenDocument (NULL, compositeHandle, docOffset, length);
-
- }
-
- Doc_SaveUndo( theDoc );
- Doc_SetDirty( theDoc, true );
-
- //clipDoc = CAGetClipboardDocument(theDoc->partDocRef, kCAClonePaste);
-
- // Add the content
- Doc_AddItemsFromCAScrap( theDoc, scrapHandle, &(theDoc->insertionPoint), transferDoc,
- kCAClonePaste );
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
-
- // Cleanup
- DisposeHandle( scrapHandle );
- DisposeHandle (compositeHandle);
-
- if (transferDoc)
- CACloseDocument (transferDoc);
-
- }
-
-
- // kODScrapTypeODBentoContainer indicates an OD container
-
- // Note: for the CALib friendly version of clipboard operations, CASample writes
- // the kCACASScrapType data directly to the system scrap, so if there
- // is a container on the clipboard it definitely is not a proxy doc,
- // so we check only for a pict part to incorporate, embed anything else.
- else if (CAUtil_ScrapHasType (kCAScrapTypeODBentoContainer, &scrapOffset, &length))
- {
-
- CADocumentRef clipDoc;
- CASize scrapSize;
- char pictTypeStr[256];
-
- // Set up PICT value name
- strcpy (pictTypeStr, kODAppleScrapTypePrefix);
- strcat (pictTypeStr, "PICT");
-
- // Get the container stream
- scrapHandle = NewHandle( 0L );
- scrapLength = GetScrap( scrapHandle, kCAScrapTypeODBentoContainer, &scrapOffset );
-
- // Open it
- clipDoc = CAOpenDocument (NULL, scrapHandle, 0, 0);
-
- if (CAUseKind (clipDoc, pictTypeStr, 0))
- {
-
- // incoroporate PICT
- CASetOffset (clipDoc, 0);
- scrapSize = CAGetSize (clipDoc);
-
- thePicture = (PicHandle) NewHandle (scrapSize);
- HLock ((Handle) thePicture);
- CARead (clipDoc, (char*) *thePicture, &scrapSize);
- HUnlock ((Handle)thePicture);
-
- elem = Doc_AddItemPict(theDoc, thePicture, theDoc->insertionPoint);
-
- Elem_SetSelected (elem, true);
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- KillPicture ( thePicture );
-
- }
- else
- {
- CAUtil_EmbedODContainerFromCADocument(theDoc, clipDoc, theDoc->insertionPoint);
- }
-
- CACloseDocument (clipDoc);
-
- }
-
-
-
- // Now check the scrap for a picture
-
- else if (CAUtil_ScrapHasType (kPICTType, &scrapOffset, &scrapLength))
- {
-
- thePicture = (PicHandle) NewHandle (0);
- scrapLength = GetScrap( (Handle) thePicture, kPICTType, &scrapOffset );
-
- // Get the PICT on the clipboard
- // if we got the picture, continue
- Doc_SaveUndo( theDoc );
- Doc_SetDirty( theDoc, true );
-
- elem = Doc_AddItemPict( theDoc, thePicture, theDoc->insertionPoint);
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- KillPicture( thePicture );
- }
-
- else
- {
- SysBeep(2);
- }
-
- }
-
- #else
-
- //---------------------------------------------------------------------------
- // Doc_PasteMenu - CALib savvy version
- //
- // first check for a scrap type of 'odcn', if present, examine the kind's
- // available from the CADocument.
-
- void Doc_PasteMenu(
- DocPtr theDoc )
- {
- PicHandle thePicture;
- Handle scrapHandle;
- long scrapLength, scrapOffset;
- long scrapOff = 0;
- ElemPtr elem;
- short i;
-
- // if the file is locked, beep and get out.
- // $$$$$ the paste menu should be disabled if the file is locked, so
- // we should never get here.
- if (theDoc->fileLocked)
- {
- SysBeep(2);
- return;
- }
-
-
- #ifdef USE_CALIB
-
- // kODScrapTypeODBentoContainer indicates the OD Clipboard.
-
- if (CAUtil_ScrapHasType (kCAScrapTypeODBentoContainer, &scrapOffset, &scrapLength))
- {
-
- CADocumentRef clipDoc;
- short numKinds;
- CAISOStr partKind;
- CASize scrapSize;
-
- // Get the container stream
- scrapHandle = NewHandle( 0L );
- scrapLength = GetScrap( scrapHandle, kCAScrapTypeODBentoContainer, &scrapOffset );
-
- // Open the container
- clipDoc = CAOpenDocument (NULL, scrapHandle, 0, scrapLength);
-
- numKinds = CAGetKindCount (clipDoc);
-
- for (i = 1; i <= numKinds; ++i)
- {
-
- char pictTypeStr[256];
-
- strcpy (pictTypeStr, kODAppleScrapTypePrefix);
- strcat (pictTypeStr, "PICT");
-
- // Make incorporate or embed decision
-
- partKind = CAGetNthKind (clipDoc, i);
-
- // is it CASample's scrap type
- if (bCStrCmp (partKind, kCACASScrapTypeStr))
- {
-
- CAUseKind (clipDoc, partKind, 0);
-
- CASetOffset (clipDoc, 0);
- scrapSize = CAGetSize (clipDoc);
-
- scrapHandle = NewHandle (scrapSize);
- CARead (clipDoc, *scrapHandle, &scrapSize);
-
- Doc_AddItemsFromCAScrap( theDoc, scrapHandle, &(theDoc->insertionPoint), clipDoc,
- kCAClonePaste );
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- DisposeHandle( scrapHandle );
-
-
- }
-
- // is it a PICT
- else if (bCStrCmp (partKind, pictTypeStr))
- {
-
- CAUseKind (clipDoc, partKind, 0);
-
- CASetOffset (clipDoc, 0);
- scrapSize = CAGetSize (clipDoc);
-
- thePicture = (PicHandle) NewHandle (scrapSize);
- HLock ((Handle) thePicture);
- CARead (clipDoc, (char*) *thePicture, &scrapSize);
- HUnlock ((Handle)thePicture);
-
- elem = Doc_AddItemPict(theDoc, thePicture, theDoc->insertionPoint);
-
- Elem_SetSelected (elem, true);
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- KillPicture ( thePicture );
-
-
- }
-
- // Can't incorporate, so embed
- else
- {
- CAUtil_EmbedODContainerFromCADocument(theDoc, clipDoc, theDoc->insertionPoint);
- }
-
- DisposePtr (partKind);
-
- }
-
- CACloseDocument (clipDoc);
-
- }
-
-
- // No OpenDoc clipboard, check for other types.
- else if (CAUtil_ScrapHasType (kCACASScrapType, &scrapOffset, &scrapLength))
- {
- CADocumentRef clipDoc;
-
- scrapHandle = NewHandle( 0L );
- scrapLength = GetScrap( scrapHandle, kCACASScrapType, &scrapOffset );
-
- Doc_SaveUndo( theDoc );
- Doc_SetDirty( theDoc, true );
-
- clipDoc = CAGetClipboardDocument(theDoc->partDocRef, kCAClonePaste);
-
- Doc_AddItemsFromCAScrap( theDoc, scrapHandle, &(theDoc->insertionPoint), clipDoc,
- kCAClonePaste );
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- DisposeHandle( scrapHandle );
- CACloseDocument (clipDoc);
-
- }
-
-
-
- #else
-
- if (CAUtil_ScrapHasType (kCASScrapType, &scrapOffset, &scrapLength))
- {
- scrapHandle = NewHandle( 0L );
- scrapLength = GetScrap( scrapHandle, kCASScrapType, &scrapOffset );
-
- Doc_SaveUndo( theDoc );
- Doc_SetDirty( theDoc, true );
-
- Doc_AddItemsFromScrap( theDoc, scrapHandle, &(theDoc->insertionPoint) );
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
-
- DisposeHandle( scrapHandle );
-
- }
-
- #endif
-
- // Now check the scrap for a picture
-
- else if (CAUtil_ScrapHasType (kPICTType, &scrapOffset, &scrapLength))
- {
-
- thePicture = (PicHandle) NewHandle (0);
- scrapLength = GetScrap( (Handle) thePicture, kPICTType, &scrapOffset );
-
- // Get the PICT on the clipboard
- // if we got the picture, continue
- Doc_SaveUndo( theDoc );
- Doc_SetDirty( theDoc, true );
-
- elem = Doc_AddItemPict( theDoc, thePicture, theDoc->insertionPoint);
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- KillPicture( thePicture );
- }
-
- else
- {
- SysBeep(2);
- }
-
- }
-
- #endif
-
-
- //---------------------------------------------------------------------------
- // Doc_ClearMenu - clear the selection from the document.
- // assume it's our window and there is a selection.
-
- void Doc_ClearMenu(
- DocPtr theDoc )
- {
- // if the file is locked, beep and get out.
- // $$$$$ the clear menu should be disabled if the file is locked, so
- // we should never get here.
- if (theDoc->fileLocked)
- SysBeep(2);
- else
- {
- Doc_SaveUndo( theDoc );
-
- Doc_SetDirty( theDoc, true );
- Doc_DeleteCurrentSelection( theDoc );
-
- Win_ShowSelection( Doc_GetWindow( theDoc ) );
- }
- }
-
- //---------------------------------------------------------------------------
- // Doc_SelectAllMenu -
- // assume it's our window and there is at least one item.
-
- void Doc_SelectAllMenu(
- DocPtr theDoc )
- {
- Doc_SelectAllElements( theDoc, true );
- }
-
-
- //---------------------------------------------------------------------------
- // Doc_RectDocToGlobal -
-
- void Doc_RectDocToGlobal(
- DocPtr theDoc,
- Rect *theRect )
- {
- GrafPtr savedPort;
- WindowPtr theWindow;
- Point saveOrigin;
-
- theWindow = Doc_GetWindow( theDoc );
-
- GetPort( &savedPort );
- SetPort( theWindow );
-
- GetOrigin( &saveOrigin );
- Doc_SetGrafOrigin( theDoc );
-
- RectLocalToGlobal( theRect );
-
- SetOrigin( saveOrigin.h, saveOrigin.v );
- SetPort( savedPort );
- }
-
- //---------------------------------------------------------------------------
- // Doc_RgnDocToGlobal -
-
- void Doc_RgnDocToGlobal( DocPtr theDoc, RgnHandle theRgn )
- {
- GrafPtr savedPort;
- WindowPtr theWindow;
- Point saveOrigin;
-
- theWindow = Doc_GetWindow( theDoc );
-
- GetPort( &savedPort );
- SetPort( theWindow );
-
- GetOrigin( &saveOrigin );
- Doc_SetGrafOrigin( theDoc );
-
- RgnLocalToGlobal( theRgn );
-
- SetOrigin( saveOrigin.h, saveOrigin.v );
- SetPort( savedPort );
- }
-
- void Doc_HandleSelectionRect (DocPtr theDoc, Point startPt)
- {
- WindowPtr window;
- Rect selectionBox;
- Rect oldBox;
- Point theLoc, localStartPt;
- Point lastLoc = {0,0};
- GrafPtr savePort;
- short count = 0, i;
- ElemPtr elem = NULL;
- Rect elemRect;
-
- if (!WaitMouseMoved( startPt ))
- return;
-
- window = Doc_GetWindow (theDoc);
-
- GetPort(&savePort);
- SetPort(window);
-
- localStartPt = startPt;
- GlobalToLocal (&localStartPt);
-
- SetRect (&oldBox, 0,0,0,0);
-
- PenMode(patXor);
- PenPat(&(qd.gray));
-
- while (WaitMouseUp())
- {
- GetMouse(&theLoc);
-
- if (!EqualPt (theLoc, lastLoc))
- {
- Pt2Rect (theLoc, localStartPt, &selectionBox);
-
- FrameRect(&oldBox);
- FrameRect(&selectionBox);
-
- lastLoc = theLoc;
- oldBox = selectionBox;
- }
- }
-
- FrameRect(&oldBox);
-
- PenPat(&(qd.gray));
- PenMode(patCopy);
-
- count = ElemColl_GetCount (theDoc->contentColl);
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- Elem_GetLocatedRect (elem, &elemRect);
-
- if ( PtInRect ( *((Point*) &(elemRect.top)), &oldBox) &&
- PtInRect ( *((Point*) &(elemRect.bottom)), &oldBox))
- {
-
- Elem_SetSelected (elem, true);
-
- }
-
- }
-
- SetPort (savePort);
- }
-
-
- //===========================================================================
- #pragma mark ' Frame management
- //===========================================================================
-
- #ifdef USE_CALIB
-
- ElemPtr Doc_AddEmbeddedFrame (DocPtr theDoc, CAFrameRef frameRef, Point location)
- {
- RgnHandle theRgn;
- ElemPtr elem;
- Rect theRect;
- Handle elemData;
-
- theRgn = CAGetFrameRgn (theDoc->partDocRef, frameRef);
- theRect = (**theRgn).rgnBBox;
-
- elem = Elem_New ((Handle)frameRef, kFrameElemType, theRect, location);
- ElemColl_AddElem (theDoc->contentColl, elem);
-
- return (elem);
-
- }
-
- //---------------------------------------------------------------------------
-
- ElemPtr Doc_FindElemForFrameRef (DocPtr theDoc, CAFrameRef frameRef)
- {
- long i;
- short count = 0;
- ElemPtr elem = NULL;
- Boolean matched = false;
-
- count = ElemColl_GetCount (theDoc->contentColl);
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
- if (frameRef == (CAFrameRef) elem->elemData)
- {
- matched = true;
- break;
- }
-
- }
-
- if (!matched) elem = NULL;
- return (elem);
-
- }
-
- #endif
-
- //---------------------------------------------------------------------------
- // Doc_CreateCAScrapFromSelection() - Generates and returns a handle to
- // scrap data. If the selection consists of a single embedded frame
- // then a NULL handle is returned.
- //
- // CAScrap stream format:
- //
- // Selection Rect Origin (Point)
- // Selection Element Count (long)
- // n Content Element Streams
- //
- Handle Doc_CreateCAScrapFromSelection(
- DocPtr theDoc, CADocumentRef transferDoc, CACloneKind cloneKind )
- {
- Handle scrapData;
- Ptr scrapOffset;
- Rect boundsRect;
- Point origin;
- OSErr theErr;
- long scrapSize, i;
- IOStream ioStream;
- CAFrameRef newFrameRef;
- ElemPtr frameElem;
- long frameCount = 0;
- short count = 0;
- ElemPtr elem = NULL;
- long selectedElemCount = 0;
- long selectedFrameCount = 0;
- Boolean isRoot = false;
- CACloneKey key = NULL;
-
- scrapSize = 0L;
-
- Doc_CalcSelectionBounds( theDoc, &boundsRect );
- origin.h = boundsRect.left;
- origin.v = boundsRect.top;
-
- // Compute the size in bytes needed for the scrap
-
- scrapSize = sizeof (Point); // Origin
- scrapSize += sizeof (long); // element count
-
- count = ElemColl_GetCount (theDoc->contentColl);
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem))
- {
- ++selectedElemCount;
- scrapSize += Elem_SpaceRequired (elem);
- if (elem->elemType == kFrameElemType) ++selectedFrameCount;
- }
- }
-
-
- // Allocate the scrap buffer
-
- scrapData = NewHandle( scrapSize );
- theErr = MemError();
- if (theErr != noErr)
- return nil;
-
- scrapOffset = *scrapData;
-
- BlockMoveData( &origin, scrapOffset, sizeof(Point) );
- scrapOffset += sizeof(Point);
-
- BlockMoveData( &selectedElemCount, scrapOffset, (long)sizeof(long) );
- scrapOffset += sizeof(long);
-
- ioStream.type = memoryStream;
- ioStream.theDoc = theDoc;
- ioStream.buffer = (Ptr) scrapOffset;
-
- // Write item data
-
- #ifdef USE_CALIB
-
- if ((selectedElemCount == 1) && (selectedFrameCount == 1))
- {
- isRoot = true;
- key = CABeginClone (theDoc->partDocRef, transferDoc, cloneKind);
- }
- else if (selectedFrameCount > 0)
- {
- key = CABeginClone (theDoc->partDocRef, transferDoc, cloneKind);
- }
- #endif
-
- for (i=1; i <= count; i++)
- {
- elem = ElemColl_GetNthElem (theDoc->contentColl, i);
-
- if (Elem_GetSelected (elem) && elem->elemType != kFrameElemType)
- {
- Elem_Write (elem, &ioStream);
- }
- else if (Elem_GetSelected (elem))
- {
- newFrameRef = CACloneFrameRef ((CAFrameRef)(elem->elemData), theDoc->partDocRef,
- transferDoc, isRoot, key);
-
- frameElem = Elem_New ((Handle) newFrameRef, kFrameElemType, elem->contentRect, elem->location);
- Elem_Write (frameElem, &ioStream);
- DisposePtr ((Ptr) frameElem);
- }
-
-
-
- }
-
- #ifdef USE_CALIB
-
- if (key)
- CAEndClone (theDoc->partDocRef, transferDoc, key);
-
- if (isRoot)
- {
- DisposeHandle( scrapData );
- scrapData = NULL;
- }
-
- #endif
-
- return scrapData;
-
- }
-
- //---------------------------------------------------------------------------
- // Doc_AddItemsFromScrap -
- //
- // origin specifies the top-left corner of the new selection rect
-
- void Doc_AddItemsFromCAScrap(
- DocPtr theDoc,
- Handle theScrap,
- Point* origin,
- CADocumentRef transferDoc,
- CACloneKind cloneKind)
- {
- Ptr offsetPtr;
- Point location, sourceOrigin;
- long i;
- Boolean lockState;
- ElemPtr elem;
- short count;
- long elemCount;
- IOStream ioStream;
- CACloneKey key = NULL;
- CAFrameRef newFrameRef;
- CATransform extTransform;
- CAVisFrame visFrame;
- ElemCollPtr tempColl;
-
- lockState = HGetState (theScrap);
- HLock (theScrap);
-
- offsetPtr = *theScrap;
- BlockMoveData( offsetPtr, (Ptr)&sourceOrigin, (long)sizeof(Point) );
- offsetPtr += sizeof(Point);
-
- BlockMoveData( offsetPtr, (Ptr)&elemCount, (long)sizeof(long) );
- offsetPtr += sizeof(long);
-
- Doc_SelectAllElements( theDoc, false );
-
- ioStream.type = memoryStream;
- ioStream.theDoc = theDoc;
- ioStream.buffer = (Ptr) offsetPtr;
-
- // Create list to hold transfer elemPtr's
-
- tempColl = (ElemCollPtr) NewPtr (sizeof (ElemColl));
- ElemColl_Init (tempColl, NULL);
-
- // Read in the elements
- for (i=0; i<elemCount; ++i)
- {
-
- // Read the element from ioStream
- elem = Elem_Read (&ioStream);
- ElemColl_AddElem (theDoc->contentColl, elem);
-
- ElemColl_AddElemTemp (tempColl, elem);
-
- // Clone the CAFrameRef
-
- #ifdef USE_CALIB
- if (elem->elemType == kFrameElemType)
- {
-
- if (!key)
- key = CABeginClone (transferDoc, theDoc->partDocRef, cloneKind);
-
- newFrameRef = CACloneFrameRef ((CAFrameRef)elem->elemData, transferDoc,
- theDoc->partDocRef, false, key);
-
- elem->elemData = (Handle) newFrameRef;
-
- }
- #endif
-
- // Set the new location
-
- Elem_GetLocation (elem, &location);
- if (origin)
- {
- location.h = location.h - sourceOrigin.h + (*origin).h;
- location.v = location.v - sourceOrigin.v + (*origin).v;
- }
- Elem_SetLocation (elem, location);
-
-
- }
-
- #ifdef USE_CALIB
- if (key)
- CAEndClone (transferDoc, theDoc->partDocRef, key);
- #endif
-
- count = ElemColl_GetCount (tempColl);
-
- for (i = 1; i<= elemCount; ++i)
- {
- elem = ElemColl_GetNthElem (tempColl, i);
-
- // Set the frame as visible
- Elem_SetVisible (elem, true);
- Elem_SetSelected (elem, true);
- }
-
- ElemColl_Free (tempColl);
- DisposePtr ((Ptr) tempColl);
-
- Doc_SetDirty( theDoc, true );
- HSetState (theScrap, lockState);
-
- }
-
-
- void GetPlatformScrapTypeList (ResType typeArray[])
- {
-
- Handle hData = NULL;
- long scrapOffset = 0;
- PScrapStuff scrapInfo;
- Handle hScrap;
- short i = 0;
-
- LoadScrap();
-
- scrapInfo = InfoScrap();
- hScrap = scrapInfo->scrapHandle;
-
- while ( scrapOffset < scrapInfo->scrapSize )
- {
- long scrapTypeLength;
- ResType theType;
- long realOffset;
-
- // There is more in the scrap
- theType = *(ResTypePtr)(((long) *hScrap) + scrapOffset);
- scrapTypeLength = GetScrap((Handle) hData, theType, (long*) &realOffset);
- typeArray[i++] = theType;
-
- if ( realOffset < scrapOffset ) break;
-
- // The length must be EVEN!
- if (scrapTypeLength & 1)
- {
- scrapTypeLength += 1;
- }
- scrapOffset = realOffset + scrapTypeLength;
- }
-
-
-
- }
-
-
-